//
//  CZRelayView.swift
//  weibo
//
//  Created by 丁蒙 on 16/6/29.
//  Copyright © 2016年 丁蒙. All rights reserved.
//

import UIKit
import SnapKit

class CZRelayView: UIView {
    //转发微博底部约束
    private var relayViewBottomConstraint: Constraint?
    
    
    var status: CZStatus? {
        didSet {
            contentLabel.text = status?.retweeted_status?.text
            
            relayViewBottomConstraint?.uninstall()
            
            if status?.retweeted_status?.pic_urls?.count > 0 {
                pictureView.hidden = false
                pictureView.pic_urls = status?.retweeted_status?.pic_urls
                self.snp_makeConstraints { (make) in
                    relayViewBottomConstraint = make.bottom.equalTo(pictureView).offset(statusMargin).constraint
                }
            } else {
                pictureView.hidden = true
                self.snp_makeConstraints(closure: { (make) in
                    relayViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(statusMargin).constraint
                })
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK - 设置视图
    private func setupUI() {
        self.backgroundColor = RGB(237, green: 237, blue: 237)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        contentLabel.snp_makeConstraints { (make) in
            make.top.equalTo(self).offset(statusMargin)
            make.left.equalTo(self).offset(statusMargin)
        }
        pictureView.snp_makeConstraints { (make) in
            make.left.equalTo(self).offset(statusMargin)
            make.top.equalTo(contentLabel.snp_bottom).offset(statusMargin)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        self.snp_makeConstraints { (make) in
            relayViewBottomConstraint = make.bottom.equalTo(pictureView).offset(statusMargin).constraint
        }
        
    }
    
    //内容
    private lazy var contentLabel: UILabel = UILabel(fontSize: 14, textColor: UIColor.lightGrayColor(),maxWidth: SCREENW - 2 * statusMargin)
    //转发微博配图
    private lazy var pictureView: CZPictureView = CZPictureView()
    
}
